﻿Imports System.IO
Imports ControleMatricula.Negocio

Public Class FrmEspecifica

    Public Const strIncluirConfirmar As String = "Confirmar &Inclusão"
    Public Const strIncluir As String = "&Incluir"
    Public Const strAlterarConfirmar As String = "Confirmar &Alteração"
    Public Const strAlterar As String = "&Alterar"
    Dim db As New DataClassesDataContext()
    Dim RowEspecifica As SPEspecificaConsultarResult
    Dim sArquivo As String
    Dim oDSLogoMarca As DataSet
    Dim oAssinaturaMemStream As New System.IO.MemoryStream()

    Private Sub btnConsultar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConsultar.Click

        Dim FrmEspecificaConsultar As FrmEspecificaConsultar
        FrmEspecificaConsultar = New FrmEspecificaConsultar
        FrmEspecificaConsultar.Owner = Me
        FrmEspecificaConsultar.ShowDialog()

        If Not IsNothing(FrmEspecificaConsultar.RowEspecifica) Then
            RowEspecifica = FrmEspecificaConsultar.RowEspecifica

            txtCDEspecifica.Text = RowEspecifica.IDEspecifica
            txtDEEspecifica.Text = RowEspecifica.DEEspecifica
            STAtivo.Checked = IIf(RowEspecifica.STAtivo, True, False)
            txtCPFResp1.Text = RowEspecifica.CPFResponsavelEspecifica1
            txtNomeResp1.Text = RowEspecifica.ResponsavelEspecifica1
            txtCPFResp2.Text = RowEspecifica.CPFResponsavelEspecifica2
            txtNomeResp2.Text = RowEspecifica.ResponsavelEspecifica2
            txtDescricaoEmpresa.Text = RowEspecifica.DEEmpresa
            txtCNPJEmpresa.Text = RowEspecifica.CNPJEmpresa
            txtIEEmpresa.Text = RowEspecifica.IEEmpresa
            DTInicioAula.Text = RowEspecifica.DTInicioAula
            DTFimAula.Text = RowEspecifica.DTFimAula
            txtVLCurso.Text = RowEspecifica.VLCurso
            txtDesconto.Text = RowEspecifica.Desconto

            If Not (IsDBNull(RowEspecifica.Assinatura) OrElse RowEspecifica.Assinatura = Nothing) Then
                oAssinaturaMemStream = New System.IO.MemoryStream(RowEspecifica.Assinatura.ToArray())
                Me.oImg.Image = Image.FromStream(oAssinaturaMemStream)
            End If



            btnAlterar.Enabled = True
            btnCancelar.Enabled = True

        End If

    End Sub

    Private Sub btnIncluir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnIncluir.Click

        If btnIncluir.Text = strIncluir Then
            txtDEEspecifica.Enabled = True
            btnAlterar.Enabled = False
            btnConsultar.Enabled = False
            btnIncluir.Text = strIncluirConfirmar
            STAtivo.Enabled = True
            GroupBox3.Enabled = True
            txtCPFResp1.Enabled = True
            GroupBox4.Enabled = True
            GroupBox1.Enabled = True
            txtCPFResp2.Enabled = True
            txtAssinatura.Enabled = True
            btnConsultarAssinatura.Enabled = True
            GroupBox5.Enabled = True
            LimparCampos(Me)
            STAtivo.Checked = True
            txtAssinatura.Text = "Selecione o caminho da Assinatura."
        Else
            If Trim(txtDEEspecifica.Text) = "" Then
                MsgBox("Preenchimento do Campo Descrição obrigatório!")
                txtDEEspecifica.Focus()
                Exit Sub
            End If
            If Not Trim(txtDEEspecifica.Text) = "" Then
                Dim Retorno As New List(Of SPEspecificaConsultarResult)
                Retorno = db.SPEspecificaConsultar(0, txtDEEspecifica.Text).ToList()
                If Retorno.Count > 0 Then
                    If txtDEEspecifica.Text = Retorno(0).DEEspecifica Then
                        MsgBox("Específica já cadastrada!", MsgBoxStyle.Information)
                        txtDEEspecifica.Focus()
                        Exit Sub
                    End If
                End If
            End If

            Dim CPF2 As String
            Dim CPFResp2 As Long

            CPF2 = txtCPFResp2.Text.Replace("-", "").Replace("/", "").Replace(".", "")

            If CPF2 = "" Then
                CPFResp2 = 0
            Else
                CPFResp2 = Convert.ToString(CPF2)
            End If


            'Assinatura
            Dim oFS As New FileStream(sArquivo, FileMode.OpenOrCreate, FileAccess.Read)

            Dim oMyData(oFS.Length() - 1) As Byte

            oFS.Read(oMyData, 0, oMyData.Length)

            oFS.Close()

            'Fim Assinatura

            db.SPEspecificaIncluir(txtDEEspecifica.Text, _
                                   IIf(STAtivo.Checked = True, True, False), _
                                   Convert.ToString(txtNomeResp1.Text), _
                                   Convert.ToString(txtCPFResp1.Text.Replace("-", "").Replace("/", "").Replace(".", "")), _
                                   Convert.ToString(txtNomeResp2.Text), _
                                   Convert.ToString(txtCPFResp2.Text.Replace("-", "").Replace("/", "").Replace(".", "")), _
                                   oMyData, _
                                   Convert.ToString(txtDescricaoEmpresa.Text), _
                                   Convert.ToString(txtCNPJEmpresa.Text.Replace("-", "").Replace("/", "").Replace(".", "")), _
                                   Convert.ToString(txtIEEmpresa.Text.Replace("-", "").Replace("/", "").Replace(".", "")), _
                                   CDate(DTInicioAula.Text), _
                                   CDate(DTFimAula.Text), _
                                   CDec(txtVLCurso.Text), _
                                   CDec(txtDesconto.Text)).ToString()

            'RowEspecifica = db.SPEspecificaConsultar(0, Convert.ToString(txtDEEspecifica.Text)).ToList()

            'txtCDEspecifica.Text = RowEspecifica.IDEspecifica

            txtDEEspecifica.Enabled = False
            btnAlterar.Enabled = True
            btnIncluir.Text = strIncluir
            btnConsultar.Enabled = True
            STAtivo.Enabled = False
            GroupBox3.Enabled = False
            txtCPFResp1.Enabled = False
            GroupBox4.Enabled = False
            GroupBox1.Enabled = False
            txtCPFResp2.Enabled = False
            txtAssinatura.Enabled = False
            btnConsultarAssinatura.Enabled = False
            GroupBox5.Enabled = False

            MsgBox("Específica incluida com sucesso!", MsgBoxStyle.Information)

        End If

    End Sub

    Private Sub FrmEspecifica_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        txtDEEspecifica.Enabled = False
        btnAlterar.Enabled = False
        btnIncluir.Enabled = True
        btnConsultar.Enabled = True
        STAtivo.Enabled = False
        GroupBox3.Enabled = False
        GroupBox4.Enabled = False
        GroupBox1.Enabled = False
        GroupBox5.Enabled = False

    End Sub

    Private Sub btnCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelar.Click

        txtDEEspecifica.Enabled = False
        btnAlterar.Enabled = False
        btnIncluir.Text = True
        btnIncluir.Text = strIncluir
        btnConsultar.Enabled = True
        STAtivo.Enabled = False
        GroupBox3.Enabled = False
        GroupBox4.Enabled = False
        GroupBox1.Enabled = False
        GroupBox5.Enabled = False
        LimparCampos(Me)

    End Sub

    Private Sub btnAlterar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAlterar.Click

        If sender.text = strAlterar Then
            txtDEEspecifica.Enabled = True
            STAtivo.Enabled = True
            btnAlterar.Text = strAlterarConfirmar
            btnConsultar.Enabled = False
            btnIncluir.Enabled = False
            GroupBox3.Enabled = True
            txtCPFResp1.Enabled = True
            GroupBox4.Enabled = True
            GroupBox1.Enabled = True
            GroupBox5.Enabled = True
            txtCPFResp2.Enabled = True
            txtDEEspecifica.Focus()
        Else
            If Trim(txtDEEspecifica.Text) = "" Then
                MsgBox("Preenchimento do Campo Descrição obrigatório!")
                txtDEEspecifica.Focus()
                Exit Sub
            End If

            Dim Id As Integer
            Id = Convert.ToInt32(txtCDEspecifica.Text)

            db.SPEspecificaAlterar(Id, _
                                   txtDEEspecifica.Text, _
                                   IIf(STAtivo.Checked = True, True, False), _
                                   Convert.ToString(txtNomeResp1.Text), _
                                   Convert.ToString(txtCPFResp1.Text.Replace("-", "").Replace("/", "").Replace(".", "")), _
                                   Convert.ToString(txtNomeResp2.Text), _
                                   Convert.ToString(txtCPFResp2.Text.Replace("-", "").Replace("/", "").Replace(".", "")), _
                                   IIf(IsNothing(Me.oImg.Image), Nothing, imageToByteArray(Me.oImg.Image)), _
                                   Convert.ToString(txtDescricaoEmpresa.Text), _
                                   Convert.ToString(txtCNPJEmpresa.Text.Replace("-", "").Replace("/", "").Replace(".", "")), _
                                   Convert.ToString(txtIEEmpresa.Text.Replace("-", "").Replace("/", "").Replace(".", "")), _
                                   CDate(DTInicioAula.Text), _
                                   CDate(DTFimAula.Text), _
                                   CDec(txtVLCurso.Text), _
                                   CDec(txtDesconto.Text)).ToString()

            txtDEEspecifica.Enabled = False
            STAtivo.Enabled = False
            GroupBox3.Enabled = False
            txtCPFResp1.Enabled = False
            GroupBox4.Enabled = False
            GroupBox1.Enabled = False
            GroupBox5.Enabled = False
            txtCPFResp2.Enabled = False
            btnAlterar.Text = strAlterar
            btnConsultar.Enabled = True
            btnIncluir.Enabled = True

            MsgBox("Específica alterado com sucesso!", MsgBoxStyle.Information)

        End If

    End Sub

    Public Function imageToByteArray(ByVal Imagem As System.Drawing.Image) As Byte()

        If Not IsNothing(Imagem) Then
            'Declare uma variável do Tipo MemoryStream
            Dim ms As New System.IO.MemoryStream()

            'Carrega o memory Stream
            Imagem.Save(ms, System.Drawing.Imaging.ImageFormat.Gif)

            'retorna um array de bytes
            Return ms.ToArray()
        End If

    End Function

    Private Sub txtCPFResp1_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtCPFResp1.Validating
        Try
            If txtCPFResp1.Text <> "" Then


                If ValidarCPF(LTrim(Trim(txtCPFResp1.Text.Replace("-", "").Replace("/", "").Replace(".", "")))) = False Then
                    MsgBox("CPF inválido", MsgBoxStyle.Information, "Gestor.Net")
                    txtCPFResp1.Focus()
                    Exit Sub
                End If

                If sender.Text <> "" Then
                    sender.Text = Mid(sender.Text, 1, 3) & "." & Mid(sender.Text, 4, 3) & "." & Mid(sender.Text, 7, 3) & "-" & Mid(sender.Text, 10, 2)
                End If
            End If


            Dim Validar As Boolean
            Dim varCpfCNPJ As String
            varCpfCNPJ = txtCPFResp1.Text.Replace("-", "").Replace("/", "").Replace(".", "")
            If btnIncluir.Text = strIncluirConfirmar Then
                Validar = True
            ElseIf btnAlterar.Text = strAlterarConfirmar Then
                If varCpfCNPJ <> txtCPFResp1.Text Then
                    Validar = True
                Else
                    Validar = False
                End If
            End If



        Catch er As Exception
            MsgBox(er.Message, MsgBoxStyle.Critical, "Específica")
        End Try

    End Sub

    Private Sub txtCPFResp1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtCPFResp1.KeyPress
        Dim KeyAscii As Short = CShort(Asc(e.KeyChar))
        KeyAscii = CShort(Numeric(KeyAscii))
        If KeyAscii = 0 Then
            e.Handled = True
        End If
    End Sub

    Private Sub txtCPFResp2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtCPFResp2.KeyPress
        Dim KeyAscii As Short = CShort(Asc(e.KeyChar))
        KeyAscii = CShort(Numeric(KeyAscii))
        If KeyAscii = 0 Then
            e.Handled = True
        End If
    End Sub

    Private Sub txtCPFResp2_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtCPFResp2.Validating
        If txtCPFResp2.Text <> "" Then


            If ValidarCPF(LTrim(Trim(txtCPFResp2.Text.Replace("-", "").Replace("/", "").Replace(".", "")))) = False Then
                MsgBox("CPF inválido", MsgBoxStyle.Information, "Gestor.Net")
                txtCPFResp2.Focus()
                Exit Sub
            End If

            If sender.Text <> "" Then
                sender.Text = Mid(sender.Text, 1, 3) & "." & Mid(sender.Text, 4, 3) & "." & Mid(sender.Text, 7, 3) & "-" & Mid(sender.Text, 10, 2)
            End If
        End If


        Dim Validar As Boolean
        Dim varCpfCNPJ As String
        varCpfCNPJ = txtCPFResp2.Text.Replace("-", "").Replace("/", "").Replace(".", "")
        If btnIncluir.Text = strIncluirConfirmar Then
            Validar = True
        ElseIf btnAlterar.Text = strAlterarConfirmar Then
            If varCpfCNPJ <> txtCPFResp2.Text Then
                Validar = True
            Else
                Validar = False
            End If
        End If
    End Sub

    Private Sub btnConsultarAssinatura_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConsultarAssinatura.Click

        Dim AbrirAssinatura As New OpenFileDialog()
        Dim Abriu As DialogResult

        AbrirAssinatura.Title = "Abrir Imagem"
        AbrirAssinatura.FileName = ""
        AbrirAssinatura.Filter = "JPEG Files|*.jpg"
        AbrirAssinatura.Multiselect = False

        Abriu = AbrirAssinatura.ShowDialog

        If Abriu = DialogResult.OK Then
            sArquivo = AbrirAssinatura.FileName
            Me.oImg.Visible = True
            Me.oImg.Image = Image.FromFile(sArquivo, True)
        End If



    End Sub

    Private Sub txtCNPJEmpresa_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtCNPJEmpresa.KeyPress
        Dim KeyAscii As Short = CShort(Asc(e.KeyChar))
        KeyAscii = CShort(Numeric(KeyAscii))
        If KeyAscii = 0 Then
            e.Handled = True
        End If
    End Sub

    Private Sub txtCNPJEmpresa_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles txtCNPJEmpresa.Validating
        If txtCNPJEmpresa.Text <> "" Then


            If ValidarCNPJ(LTrim(Trim(txtCNPJEmpresa.Text.Replace("-", "").Replace("/", "").Replace(".", "")))) = False Then
                MsgBox("CNPJ inválido", MsgBoxStyle.Information, "Gestor.Net")
                txtCNPJEmpresa.Focus()
                Exit Sub
            End If

            If sender.Text <> "" Then
                sender.Text = Mid(sender.Text, 1, 2) & "." & Mid(sender.Text, 3, 3) & "." & Mid(sender.Text, 6, 3) & "/" & Mid(sender.Text, 9, 4) & "-" & Mid(sender.Text, 13, 2)
            End If
        End If


        Dim Validar As Boolean
        Dim varCpfCNPJ As String
        varCpfCNPJ = txtCNPJEmpresa.Text.Replace("-", "").Replace("/", "").Replace(".", "")
        If btnIncluir.Text = strIncluirConfirmar Then
            Validar = True
        ElseIf btnAlterar.Text = strAlterarConfirmar Then
            If varCpfCNPJ <> txtCNPJEmpresa.Text Then
                Validar = True
            Else
                Validar = False
            End If
        End If
    End Sub
End Class